package test.lyj.tool;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;


import test.lyj.R;
import android.app.AlertDialog;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract.RawContacts;
import android.provider.MediaStore.Images.Media;
import android.util.Log;

public class Tool{
	/**
	 * 正式服务器地址
	 */
	public static final String SERVICE_URL ="http://sh.e7uni.com/cardbox/businesscard/androidService";
	public static final String testcurve_URL ="http://192.168.0.103:8080/gc/curve/json.action";
	
	/**
	 * 线程状态 【 超时】
	 */
	public static final int TIMER_TASK_TIMEOUT=-1;
	/**
	 * 线程状态 【完成】
	 */
	public static final int TIMER_TASK_COMPELETE=1;
	public static final int  TIMER_TASK_LINKEDTIME=2;
	public static final int  TIMER_TASK_SHOWTITLE=3;
	public static final int  TIMER_TASK_INFO=4;
	/**
	 * 线程执行完成以后，返回值的变量名。
	 */
	public static final String TIMER_TASK_RESULT="runResult";

	public static final boolean DEBUG = true;
	public static final boolean showDebugTitle = true;
    /**
     * 全局静态变量
     */
    public static String username=null;
    public static String psw=null;
    public static void showMsgDialog(Context c, CharSequence msg) {
			new AlertDialog.Builder(c).setTitle("信息提示！").setMessage(msg).setIcon(R.drawable.icon)
					.setPositiveButton("你的真的确定？", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialoginterface, int i) {
						}
			}).show();
		}
	
	public static ArrayList<HashMap<String,Object>> getStaticMap(Context c,int start,int end){
		ArrayList<HashMap<String,Object>> temp=new ArrayList();
		for (int i = 0; i < 21; i++) {
			
		
		HashMap<String, Object> testDataMap=new HashMap<String, Object>();
		testDataMap.put("name", "name"+i);
		testDataMap.put("address", "address"+i);
		Bitmap ic;
		if (i%2==0) {
			ic = BitmapFactory
					.decodeResource(c.getResources(), R.drawable.icon);
		}else{
			ic = BitmapFactory.decodeResource(c.getResources(),R.drawable.line);
			
		}
		testDataMap.put("icon", ic);
		temp.add(testDataMap);
		}
		ArrayList<HashMap<String,Object>> result=new ArrayList();
		for (int i  =start; i<=end; i++) {
			result.add(temp.get(i));
		}
		return result;
	}
	
	public static String URLBase64encode(String s) {
		return URLEncoder.encode(Base64encode(s));
	}

	public static boolean isBlank(String s) {
		return s==null||"".equals(s);
	}
	public static String Base64encode(String s) {
		try {
			String d = HBase64.encode(s.getBytes("GBK"));
			return d;
		} catch (UnsupportedEncodingException e) {
			// 不抛出
		}
		return s;
	}
	public static String Base64decode(String s) {
		try {
			return new String(HBase64.decode(s), "GBK");
		} catch (UnsupportedEncodingException e) {
		}
		return s;
	}
	
	public static void addImg2DB(Context c,String name,Bitmap bm ){
		ContentResolver cr = c.getContentResolver();
//		String name = cur.getString(cur.getColumnIndex(Media.DISPLAY_NAME));
//		String path = cur.getString(cur.getColumnIndex(Media.DATA));
		Media.insertImage(cr, bm, "testbmp", "its test bmp");
		
		
	}
	/**
	 * 简单创建联系人，只保存用户名跟手机
	 * 
	 * @param c
	 * @param name
	 * @param mobile
	 */
	public static void addSimpleContact(Context c, String name,String mobile){
			ContentResolver cr = c.getContentResolver();
			ContentValues cv = new ContentValues();
			cv.put(Contacts.People.NAME, name);
			// cv.put(Contacts.People.NUMBER, 12345678);
			 // 映射关系:1 = 新的联系方式加入 favorites,0 = 新的联系方式不是加入 favorites
			cv.put(Contacts.People.STARRED, 0);// 很重要
			Uri uri = Contacts.People.createPersonInMyContactsGroup(cr, cv);
			Uri phoneuri = Uri.withAppendedPath(uri,
					Contacts.People.Phones.CONTENT_DIRECTORY);
			cv.clear();
			cv.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
			cv.put(Contacts.Phones.NUMBER, mobile);
			cr.insert(phoneuri, cv);
	}


}